<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    >
      <template #btns>
        <el-button :loading="exLoading" type="primary" @click="handleEx">{{
          translate("export")
        }}</el-button>
      </template>
    </QueryForm>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        v-for="item in propList"
        :key="item.label"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :min-width="item.minWidth"
        show-overflow-tooltip
      />
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { sysRechargeRecord, sysRechargeRecordEx } from '@/api'
import { propList, queryFormConfig } from './config'
export default {
  data() {
    return {
      propList,
      queryFormConfig,
      listLoading: false,
      exLoading: false,
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {}
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleEx() {
      const { filters: param } = this
      if (param.selTime && param.selTime.length > 0) {
        param.startTime = param.selTime[0]
        param.endTime = param.selTime[1]
      } else {
        param.startTime = null
        param.endTime = null
      }
      this.exLoading = true
      sysRechargeRecordEx({ param: { ...param } }).then(res => {
        this.exLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exLoading = false
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (param.selTime && param.selTime.length > 0) {
        param.startTime = param.selTime[0]
        param.endTime = param.selTime[1]
      } else {
        param.startTime = null
        param.endTime = null
      }
      this.listLoading = true
      sysRechargeRecord({ pagination, param: { ...param } }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style>
</style>
